package com.wgchao.identityrightsmanagement.application.controller;

import com.wgchao.identityrightsmanagement.application.service.DepartmentApplicationService;
import com.wgchao.identityrightsmanagement.domain.organization.model.DepartmentCreator;
import com.wgchao.identityrightsmanagement.domain.organization.model.DepartmentUpdator;
import com.wgchao.identityrightsmanagement.infrastructure.utils.PageData;
import com.wgchao.identityrightsmanagement.infrastructure.utils.ResponseData;
import com.wgchao.identityrightsmanagement.query.DepartmentQuery;
import com.wgchao.identityrightsmanagement.query.pageparam.DepartmentPageParam;
import com.wgchao.identityrightsmanagement.query.vo.DepartmentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @ClassName: DepartmentController
 * @author: wgchao
 * @createTime: 2020/6/15 3:59 PM
 * @Description: 部门管理
 */
@RestController
@RequestMapping("/sntk/sys/base/department")
@Api(value = "DepartmentController", description = "部门信息管理")
public class DepartmentController {
    @Autowired
    private DepartmentApplicationService departmentApplicationService;
    @Autowired
    private DepartmentQuery departmentQuery;

    @ApiOperation(value = "向机构添加部门")
    @PostMapping("/{agencyCode}")
    public ResponseData addDepartmentToAgency(@ApiParam(name = "agencyCode", value = "机构编号", required = true) @PathVariable String agencyCode,
                                           @ApiParam(name = "creator", value = "部门信息", required = true) @RequestBody DepartmentCreator creator){
        departmentApplicationService.addDepartment(agencyCode, creator, "admin");
        return ResponseData.success();
    }

    @ApiOperation(value = "修改部门基本信息")
    @PutMapping("/{code}")
    public ResponseData modifyDepartmentInfo(@ApiParam(name = "code", value = "部门编号", required = true) @PathVariable String code,
                                          @ApiParam(name = "updator", value = "部门信息", required = true) @RequestBody DepartmentUpdator updator){
        departmentApplicationService.modifyDepartmentInfo(code, updator, "admin");
        return ResponseData.success();
    }

    @ApiOperation(value = "禁用部门")
    @PutMapping("/disabled/{code}")
    public ResponseData disabled(@ApiParam(name = "code", value = "部门编号", required = true) @PathVariable String code){
        departmentApplicationService.disable(code, "admin");
        return ResponseData.success();
    }

    @ApiOperation(value = "启用部门")
    @PutMapping("/regain/{code}")
    public ResponseData regain(@ApiParam(name = "code", value = "部门编号", required = true) @PathVariable String code){
        departmentApplicationService.regain(code, "admin");
        return ResponseData.success();
    }

    @ApiOperation(value = "删除部门")
    @DeleteMapping("/{code}")
    public ResponseData remove(@ApiParam(name = "code", value = "部门编号", required = true) @PathVariable String code){
        departmentApplicationService.remove(code, "admin");
        return ResponseData.success();
    }

    @ApiOperation(value = "查询部门信息")
    @GetMapping("/{code}")
    public ResponseData<DepartmentVo> findByCode(@ApiParam(name = "code", value = "部门编号", required = true) @PathVariable String code){
        DepartmentVo departmentVo = departmentQuery.findByCode(code);
        return ResponseData.success().setData(departmentVo);
    }

    @ApiOperation(value = "查询部门信息")
    @GetMapping("/page/condition")
    public ResponseData<PageData<DepartmentVo>> findByConditions(@ApiParam(name = "departmentPageParam", value = "查询条件", required = true)
                                                       DepartmentPageParam departmentPageParam){
        PageData<DepartmentVo> departmentVoPageData = departmentQuery.findByConditions(departmentPageParam);
        return ResponseData.success().setData(departmentVoPageData);
    }

    @ApiOperation(value = "向机构添加职位")
    @PostMapping("/assign/offices/to/{code}")
    public ResponseData assignOfficesToDepartment(@ApiParam(name = "code", value = "部门编号", required = true) @PathVariable String code,
                                                  @ApiParam(name = "officeCodes", value = "职位信息", required = true) @RequestBody List<String> officeCodes){
        departmentApplicationService.assignOfficesToDepartment(officeCodes, code, "admin");
        return ResponseData.success();
    }

}
